#ifndef IMAGE_UTILS_H
#define IMAGE_UTILS_H

#include <QImage>
#include <QPixmap>
#include <QPainter>
#include <QString>
#include <QFont>
#include <QFontMetrics>

class ImageUtils
{
public:

    // adds a black outline to the image
    static void AddOutline(QImage& img)
    {
        int w = img.width(), h = img.height();
        QRgb pc;
        QRgb black = qRgba(0, 0, 0, 255);
        bool outside = true;

        // top-to-bottom loop
        for(int i=0; i<w; i++)
        {
            outside = true;
            for(int j=0; j<h; j++)
            {
                pc = img.pixel(i, j);
                if(qAlpha(pc) > 0 && outside)
                {
                    img.setPixel(i, j, black);
                    outside = false;
                }

                if(qAlpha(pc) == 0 && !outside)
                {
                    img.setPixel(i, j-1, black);
                    outside = true;
                }

                if(j == h-1 && !outside)
                {
                    img.setPixel(i, j, black);
                }
            }
        }

        // left-to-right loop
        for(int j=0; j<h; j++)
        {
            outside = true;
            for(int i=0; i<w; i++)
            {
                pc = img.pixel(i, j);
                if(qAlpha(pc) > 0 && outside)
                {
                    img.setPixel(i, j, black);
                    outside = false;
                }

                if(qAlpha(pc) == 0 && !outside)
                {
                    img.setPixel(i-1, j, black);
                    outside = true;
                }

                if(i == w-1 && !outside)
                {
                    img.setPixel(i, j, black);
                }
            }
        }
    }

    // assign the given alpha value to all the non-alpha image pixels
    static void BlendCheck(QImage& img, int alpha)
    {
        if(alpha == 0) alpha = 1;
        int imgW = img.width(), imgH = img.height();
        QRgb c, blended;

        for(int i=0; i<imgH; i++)
        {
            for(int j=0; j<imgW; j++)
            {
               c = img.pixel(j, i);

               if(qAlpha(c) > 0)
               {
                   blended = qRgba(qRed(c), qGreen(c), qBlue(c), alpha);
                   img.setPixel(j, i, blended);
               }
            }
        }
    }

    static int GetTextWidth(const QString& str, const QFont& font)
    {
        QFontMetrics fm(font);
        return fm.width(str);
    }

    static int GetTextHeight(const QString& str, const QFont& font)
    {
        QFontMetrics fm(font);
        int h = fm.height(), w = fm.width(str);

        QImage img(w, h, QImage::Format_Mono);
        img.fill(1);

        QPainter p(&img);
        p.setPen(Qt::black);
        p.drawText(0, h, str);

        QRgb value;

        for(int j=0; j<h; j++)
        {
            for(int i=0; i<w; i++)
            {
                value = img.pixel(i, j);
                if(qRed(value) == 0)
                {
                    return (h - j);
                }
            }
        }

        return 0;
    }

    // retrieves the given image from the ImagesManager
    static QPixmap* CreatePixmapFromImage( int w, int h, QImage* source )
    {
        QPixmap* p = new QPixmap( w, h );
        p->fill( QColor( 0, 0, 0, 0 ) );

        QPainter painter( p );
        painter.drawImage( p->rect(), *source, source->rect() );
        painter.end();

        return p;
    }
};


#endif // IMAGE_UTILS_H
